<?php
namespace Admin\Controller;

use Think\Controller;

class PaymentController extends CommonController {
	public function _initialize() {
		parent::_initialize();
		$this->title_index = "网关列表";
		$this->title_details = "网关配置";
	}
	public function index() {
		$pay_list = getFileList("./Application/Common/Payment/", 'Payment.class.php');
		$oop = M("payment");
		foreach ($pay_list as $key => $value) {
			$action = str_replace("Payment.class.php", "", $value);
			if (strlen($action) >= 1) {
				$arr = Pay("Common/" . $action . "/params");
				$arr['name'] = $action;
				if ($oop->where("pm_name='" . $action . "'")->find()) {
					$arr['status'] = 1;
				} else {
					$arr['status'] = 0;
				}

				$final[$key] = $arr;
			}
		}
		//dump($final);
		$this->assign("list", $final);
		$this->assign("main_title", $this->title_index);
		$this->display();
	}

	public function install() {
		$oop = M("payment");
		$con['pm_name'] = I("name");
		$result = $oop->where($con)->find();
		if ($result) {
			$this->error("模块已经安装过！");
		} else {
			$arr['pm_sort'] = 10;
			$arr['pm_name'] = I("name");
			if ($oop->add($arr)) {
				$this->success("安装成功！");
			} else {
				$this->error("安装失败，请重试。");
			}
		}
	}

	public function uninstall() {
		$oop = M("payment");
		$con['pm_name'] = I("name");
		$result = $oop->where($con)->delete();
		if ($result) {
			$this->success("卸载成功！");
		} else {
			$this->error("卸载失败，请重试！");
		}
	}

	public function details() {
		$oop = M("payment");
		$con['pm_name'] = I("name");
		$result = $oop->where($con)->find();
		$arr = json_decode($result['pm_data'], true);
		$arr['name'] = $result['pm_name'];
		$arr['sort'] = $result['pm_sort'];
		//dump($arr);
		$this->assign($arr);
		$this->assign("main_title", $this->title_details);
		$this->display();
	}

	public function updateHandle() {
		$oop = M("payment");
		$con['pm_name'] = I("name");
		$result = $oop->where($con)->find();
		if ($result) {
			$json_result = json_encode($_POST);
			$data['pm_sort'] = I("sort");
			$data['pm_data'] = $json_result;
			$result1 = $oop->where($con)->save($data);
			if ($result1) {
				$this->success("更新成功！");
			} else {
				$this->error("更新失败！");
			}
		} else {
			$this->error("模块尚未安装！");
		}
	}

}
